package com.huoshan.im.server.action;


import com.huoshan.im.server.action.support.JsonActionSupport;
import com.huoshan.im.server.dto.in.GeneralMessage;
import com.huoshan.im.server.dto.in.SrvSendMessage;
import com.huoshan.im.server.messaging.MessageId;
import com.huoshan.im.server.service.MessageSenderService;
import com.huoshan.im.server.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import online.chazi.netkit.annotation.Inject;
import online.chazi.netkit.messaging.Message;

/**
 * @description: 从repeater接收业务消息 回写给客户端
 * @author: 火山九日
 * @create: 2021-12-28 14:39
 **/

@Slf4j
@Inject
public class ReplyClientMessageAction extends JsonActionSupport<SrvSendMessage> {

    private MessageSenderService messageSenderService;

    @Override
    public void execute(SrvSendMessage sendMessage) {
        if (sendMessage.getToUserId() == null) {
            log.warn("回写客户端消息失败, 消息体中无toUserId, msg:{}", JsonUtils.toJson(sendMessage));
            return;
        }

        Message message = new Message();
        message.setMessageId(MessageId.MESSAGE_FROM_SERVER.id);
        message.setExtension(getExtension());
        GeneralMessage generalMessage = new GeneralMessage();
        generalMessage.setBusinessId(sendMessage.getBusinessId());
        generalMessage.setContent(sendMessage.getContent());
        message.setMessageContent(JsonUtils.toJson(generalMessage));

        messageSenderService.sendToUserUnKafka(sendMessage.getFromUserId(), sendMessage.getToUserId(), sendMessage.getToClientType(), message);
    }
}